package com.young.ums.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.young.common.core.cache.ICacheClient;
import com.young.common.util.StringUtils;
import com.young.ums.model.SsoSecure;
import com.young.ums.service.IUmsCacheService;
import com.young.ums.util.UmsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ums缓存操作
 * Created by Administrator on 2019/12/18 0018.
 */
@Service
public class UmsCacheServiceImpl implements IUmsCacheService {
    private static final Logger logger = LoggerFactory.getLogger(UmsCacheServiceImpl.class);
    /**
     * 缓存客户端
     */
    @Autowired
    ICacheClient cacheClient;

    /**
     * 获取sso安全验证信息
     *
     * @param sessionId 会话id
     * @return 安全验证信息
     */
    @Override
    public SsoSecure getSsoSecure(String sessionId) {
        SsoSecure ssoSecure =  cacheClient.get(UmsConstants.CACHE_KEY_PRE.SSO_SECURE + sessionId, new TypeReference<SsoSecure>() {});
        logger.info("[获取sso安全验证信息] 根据会话ID={}查询安全验证缓存={}", sessionId, ssoSecure);
        return ssoSecure;
    }

    /**
     * 设置安全验证信息
     *
     * @param sessionId 会话id
     * @param ssoSecure 安全验证信息
     */
    @Override
    public void setSsoSecure(String sessionId, SsoSecure ssoSecure) {
        cacheClient.save(UmsConstants.CACHE_KEY_PRE.SSO_SECURE + sessionId, ssoSecure);
    }

    /**
     * 获取账号登录失败纪录
     *
     * @param account 账号
     * @return
     */
    @Override
    public List<Long> getSsoFailList(String account) {
        return cacheClient.get(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FAIL_LIST + account, new TypeReference<List<Long>>() {});
    }

    /**
     * 设置账号登录失败纪录
     *
     * @param account  账号
     * @param failList 失败纪录
     */
    @Override
    public void setSsoFailList(String account, List<Long> failList) {
        cacheClient.save(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FAIL_LIST + account, failList);
    }

    /**
     * 删除失败纪录
     *
     * @param account 账号
     */
    @Override
    public void removeSsoFailList(String account) {
        cacheClient.del(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FAIL_LIST + account);
    }

    /**
     * 冻结账号
     *
     * @param account         账号
     * @param freezeTimestamp 冻结结束时间
     */
    @Override
    public void setAccountFreeze(String account, Long freezeTimestamp) {
        cacheClient.saveStr(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FREEZE + account, freezeTimestamp.toString());
    }

    /**
     * 获取账号冻结结束时间
     *
     * @param account
     * @return
     */
    @Override
    public Long getAccountFreeze(String account) {
        String times = cacheClient.getStr(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FREEZE + account);
        if (StringUtils.isBlank(times)) {
            return null;
        }
        return Long.valueOf(times);
    }

    /**
     * 删除冻结缓存
     *
     * @param account 账号
     */
    @Override
    public void removeAccountFreeze(String account) {
        cacheClient.delStr(UmsConstants.CACHE_KEY_PRE.SSO_ACCOUNT_FREEZE + account);
    }

    /**
     * 查询ums配置
     *
     * @param key 配置key
     * @return
     */
    @Override
    public String getUmsConfigValue(String key) {
        return cacheClient.getStr(UmsConstants.CACHE_KEY_PRE.UMS_CONFIG + key);
    }

    /**
     * 缓存ums配置
     *
     * @param key   配置key
     * @param value 配置值
     */
    @Override
    public void setUmsConfigValue(String key, String value) {
        cacheClient.saveStr(UmsConstants.CACHE_KEY_PRE.UMS_CONFIG + key, value);
    }

    /**
     * 删除ums配置
     *
     * @param key 配置key
     */
    @Override
    public void removeUmsConfigValue(String key) {
        logger.info("[删除ums配置缓存] 入参key={}", key);
        cacheClient.delStr(UmsConstants.CACHE_KEY_PRE.UMS_CONFIG + key);
    }
}
